﻿using HomagGroup.Base.UI;
using System;
using System.Collections.ObjectModel;

namespace HomagChina.Nesting
{
    public class StatisticsViewModel : HomagGroup.Base.UI.NotifyPropertyChangedBase
    {
        private DateTime _StartTime = DateTime.Now.AddMonths(-1);

        public DateTime StartTime
        { get => _StartTime; set { _StartTime = value; ViewloadedCommand(); OnPropertyChanged(); } }

        private DateTime _EndTime = DateTime.Now;

        public DateTime EndTime
        { get => _EndTime; set { _EndTime = value; ViewloadedCommand(); OnPropertyChanged(); } }

        private int _MachineType = 0;

        public int MachineType
        { get => _MachineType; set { _MachineType = value; ViewloadedCommand(); OnPropertyChanged(); } }

        private int _TimeType = 0;

        public int TimeType
        { get => _TimeType; set { _TimeType = value; ViewloadedCommand(); OnPropertyChanged(); } }

        private ObservableCollection<PerformanceData> _NestingOneBarSource;

        public ObservableCollection<PerformanceData> NestingOneBarSource
        {
            get => _NestingOneBarSource;
            set { _NestingOneBarSource = value; OnPropertyChanged(); }
        }

        private ObservableCollection<PerformanceData> _NestingTwoBarSource;

        public ObservableCollection<PerformanceData> NestingTwoBarSource
        {
            get => _NestingTwoBarSource;
            set { _NestingTwoBarSource = value; OnPropertyChanged(); }
        }

        private ObservableCollection<PerformanceData> _NestingThreeBarSource;

        public ObservableCollection<PerformanceData> NestingThreeBarSource
        {
            get => _NestingThreeBarSource;
            set { _NestingThreeBarSource = value; OnPropertyChanged(); }
        }

        public RelayCommand LoadedCommand { get; }

        public StatisticsViewModel()
        {
            LoadedCommand = new RelayCommand(ViewloadedCommand);
        }

        private void ViewloadedCommand()
        {
            Random random = new Random();
            NestingOneBarSource = new ObservableCollection<PerformanceData>();
            NestingTwoBarSource = new ObservableCollection<PerformanceData>();
            NestingThreeBarSource = new ObservableCollection<PerformanceData>();
            DateTime currentDateTime = StartTime;
            DateTime endTime = EndTime;
            while (currentDateTime <= endTime)
            {
                if (MachineType == 0)
                {
                    NestingOneBarSource.Add(new PerformanceData(currentDateTime, random.Next(1000, 100000)));
                    NestingTwoBarSource.Add(new PerformanceData(currentDateTime, random.Next(1000, 100000)));
                    NestingThreeBarSource.Add(new PerformanceData(currentDateTime, random.Next(1000, 100000)));
                }
                else if (MachineType == 1)
                {
                    NestingOneBarSource.Add(new PerformanceData(currentDateTime, random.Next(1000, 100000)));
                }
                else if (MachineType == 2)
                {
                    NestingTwoBarSource.Add(new PerformanceData(currentDateTime, random.Next(1000, 100000)));
                }
                else
                {
                    NestingThreeBarSource.Add(new PerformanceData(currentDateTime, random.Next(1000, 100000)));
                }

                switch (TimeType)
                {
                    case 0:
                        currentDateTime = currentDateTime.AddDays(1);
                        break;

                    case 1:
                        currentDateTime = currentDateTime.AddMonths(1);
                        break;

                    case 2:
                        currentDateTime = currentDateTime.AddYears(1);
                        break;
                }
            }
        }
    }
}